package personal.vivian.controller;

import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import personal.vivian.DataService;
import personal.vivian.aop.annotation.Transform;
import personal.vivian.aop.annotation.TransformParam;
import personal.vivian.constants.OperationStatus;
import personal.vivian.dto.Employee;
import personal.vivian.dto.Request;
import personal.vivian.dto.Response;
import personal.vivian.logic.DataManager;
import personal.vivian.logic.GroovyClass;
import personal.vivian.logic.GroovyCompiler;

import java.io.File;
import java.util.Map;

@RestController
@RequestMapping(value = "/employee/info" )
public class DataController {
    @Autowired
    DataService dataService;
    @Value("${groovy.path}")
    private String groovyPath;

    @Autowired
    private DataManager dataManager;

    @RequestMapping(value = "/queryEmp")
   // @ResponseBody
    public void queryEmp(@RequestParam String id){
        System.out.println("修改之后："+id);
       // return dataService.queryEmp(id);
    }
    @RequestMapping(value = "/queryEmpP")
    public Employee queryEmpGet(){
        return new Employee();
    }
    @RequestMapping(value = "/queryEmpByReq")
    @ResponseBody
    public Employee queryEmp(@RequestBody Request request){
        return dataService.queryEmp(request.getId());
    }
    @RequestMapping(value = "/saveItem")
    @Transform(returnStatus = OperationStatus.None)
    public String saveItem(@RequestParam int id){
        System.out.println("aaa");
        return null;
    }


    @RequestMapping(value = "/{fileName}")
    @ResponseBody
    public Response run(@PathVariable("fileName") String fileName,@RequestBody(required = false) Object req){
        Response response = new Response();
        Map<String,Object> binds = Maps.newHashMap();
        binds.put("req",req);
        binds.put("response",response);
        File file = new File(groovyPath+"/"+fileName+".groovy");
     /*   GroovyClassLoader groovyClassLoader=new GroovyClassLoader();
        Binding binding = new Binding(binds);
        Script script = null;
        try {

            Class<?> scriptClass =  groovyClassLoader.parseClass(file);
            script = InvokerHelper.createScript(scriptClass, binding);
        } catch (IOException e) {
            e.printStackTrace();
        }
        response.setObject(script.run());*/
        GroovyClass aClass = GroovyCompiler.getClass(file);
        Object script = aClass.getScript(binds);
        response.setObject(script);
        return response;
    }
}
